package com.example.city_home_app.RecyclerViewAdapter;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.constraint.ConstraintLayout;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.city_home_app.Activity.PostInfoActivity;
import com.example.city_home_app.Activity.UserInfoActivity;
import com.example.city_home_app.Bean.Down_Post;
import com.example.city_home_app.R;
import com.example.city_home_app.UI.MyCircleImageView;
import com.example.city_home_app.UI.UIUtils;
import com.squareup.picasso.Picasso;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

import static com.example.city_home_app.Activity.PostAddActivity.cutStringByImgTag;

public class Adapter_Post extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context context;
    private List<Down_Post> list;
    private String key = "";

    public Adapter_Post(Context context) {
        this.context = context;
    }


    public void setList(List<Down_Post> list) {
        this.list = list;
    }

    public void clearList() {
        if (list != null) {
            list.clear();
        }
    }

    public void setKey(String key) {
        this.key = key;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(context).inflate(R.layout.recitem_post, parent, false);
        Adapter_Post.ViewHolder viewHolder = new Adapter_Post.ViewHolder(itemView);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        final Down_Post bean = list.get(position);
        if (bean == null) {
            return;
        }
        final Adapter_Post.ViewHolder viewHolder = (Adapter_Post.ViewHolder) holder;

        if(bean.getPost_status()==4){
            viewHolder.tvRecitemCommunityTop.setVisibility(View.VISIBLE);
        }else{
            viewHolder.tvRecitemCommunityTop.setVisibility(View.INVISIBLE);
        }
        viewHolder.tvRecitemCommunityComment.setText("评论 " + bean.getPost_comment());
        viewHolder.tvRecitemCommunityTime.setText(bean.getPost_time());
        viewHolder.tvRecitemCommunityHit.setText("阅读 " + bean.getPost_hit());
        Picasso.with(context)
                .load("http://siminzhang.top:8088/city_home_server/public/static/posters/" + bean.getUser_headimage())
                .into(viewHolder.imgRecitemCommunityAuthor);
        List<String> list = cutStringByImgTag(bean.getPost_content());
        String content = bean.getPost_content();
        for (String s : list) {
            if (s.startsWith("<img src=")) {
                /*Pattern p_src = Pattern.compile("(src|SRC)=(\"|\')(.*?)(\"|\')");
                Matcher m_src = p_src.matcher(s);
                if (m_src.find()) {
                    String a = m_src.group(3);
                    String i = a.replace("\\","");
                    content = content.replace(s, i);
                }*/
                content = content.replace(s, "[图片]");
            }
        }

        if (!key.equals("")) {
            viewHolder.tvRecitemCommunityTitle.setText(UIUtils.matcherSearchText(context, R.color.Red, bean.getPost_title(), key));
            viewHolder.tvRecitemCommunityContent.setText(UIUtils.matcherSearchText(context, R.color.Red, content, key));
            viewHolder.tvRecitemCommunityAuthor.setText(UIUtils.matcherSearchText(context, R.color.Red, bean.getUser_name(), key));
        } else {
            viewHolder.tvRecitemCommunityAuthor.setText(bean.getUser_name());
            viewHolder.tvRecitemCommunityTitle.setText(bean.getPost_title());
            viewHolder.tvRecitemCommunityContent.setText(content);
        }
        viewHolder.layoutRecitemPost.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, PostInfoActivity.class);
                intent.putExtra("id", bean.getPost_id());
                context.startActivity(intent);
            }
        });

        viewHolder.imgRecitemCommunityAuthor.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, UserInfoActivity.class);
                intent.putExtra("id", bean.getUser_id());
                context.startActivity(intent);
            }
        });

    }

    @Override
    public int getItemCount() {
        if (list == null) {
            return 0;
        }
        return list.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_recitem_community_title)
        TextView tvRecitemCommunityTitle;
        @BindView(R.id.tv_recitem_community_content)
        TextView tvRecitemCommunityContent;
        @BindView(R.id.img_recitem_community_author)
        MyCircleImageView imgRecitemCommunityAuthor;
        @BindView(R.id.tv_recitem_community_author)
        TextView tvRecitemCommunityAuthor;
        @BindView(R.id.tv_recitem_community_time)
        TextView tvRecitemCommunityTime;
        @BindView(R.id.tv_recitem_community_hit)
        TextView tvRecitemCommunityHit;
        @BindView(R.id.tv_recitem_community_comment)
        TextView tvRecitemCommunityComment;
        @BindView(R.id.layout_recitem_post)
        ConstraintLayout layoutRecitemPost;
        @BindView(R.id.tv_recitem_community_top)
        TextView tvRecitemCommunityTop;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
